package com.central.scheduler.api.controller;

/**
 * @author Tindy
 * @date 2022/1/6
 * @describe
 */

import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysUser;
import com.central.common.model.WorkerServer;
import com.central.common.utils.UserRoleUtil;
import com.central.scheduler.api.entity.treeview.TreeViewEntity;
import com.central.scheduler.api.service.IProcessDefinitionService;
import com.central.scheduler.common.utils.SnowFlakeUtils;
import com.central.scheduler.dao.model.ProcessDefinition;
import com.central.scheduler.dao.model.ProcessDefinitionLog;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@RestController
@Api(tags = "ProcessDefinition模块api")
@Slf4j
@RequestMapping("/processDefinition")
public class ProcessDefinitionController {


    @Autowired
    private IProcessDefinitionService processDefinitionService;

    @ApiOperation(value = "创建或更新作业流程")
    @PostMapping("/saveOrUpdateProcess")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result saveOrUpdateProcess(@ApiIgnore @LoginUser SysUser user, @RequestBody ProcessDefinition processDefinition,Boolean isFromProcessDefine) throws SnowFlakeUtils.SnowFlakeException {
        return processDefinitionService.saveOrUpdateProcess(user,processDefinition,isFromProcessDefine);
    }

    @ApiOperation(value = "设置作业流owners")
    @PostMapping("/setOwners")
    @RequiredPermission({PermissionConstants.DEV_USER,PermissionConstants.PRO_ADMIN})
    public Result setOwners(@ApiIgnore @LoginUser SysUser user, @RequestBody List<String> owners, Long ProcessId) throws SnowFlakeUtils.SnowFlakeException {
        return processDefinitionService.setOwners(user,owners,ProcessId);
    }

    @ApiOperation(value = "查询作业流定义列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer",example = "1"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer",example = "10"),
            @ApiImplicitParam(name = "isTmp", value = "是否为临时", required = true, dataType = "Boolean",example = "false")
    })
    @GetMapping("/findProcesses")
    public PageResult<ProcessDefinition> findProcesses(@ApiIgnore @LoginUser SysUser user, @RequestParam Map<String, Object> params) {
        params.put("proId",user.getCurrentProjectId());
        return processDefinitionService.findProcesses(params);
    }

    @ApiOperation(value = "查询作业流版本列表")
    @GetMapping("/findProcessLogs")
    public List<Map<String,Object>> findProcessLogs(@ApiIgnore @LoginUser SysUser user,Long code) {
        return processDefinitionService.findProcessLogs(user.getCurrentProjectId(),code);
    }

    @ApiOperation(value = "查询作业流版本明细")
    @GetMapping("/findProcessLogDtl")
    public ProcessDefinitionLog findProcessLogDtl(@ApiIgnore @LoginUser SysUser user, Long code,Integer version) {
        return processDefinitionService.findProcessLogDtl(user.getCurrentProjectId(),code,version);
    }

    @ApiOperation(value = "作业流版本切换")
    @GetMapping("/switchVersion")
    public Result switchVersion(@ApiIgnore @LoginUser SysUser user,Long logId) {
        return processDefinitionService.switchVersion(user,logId);
    }

    @ApiOperation(value = "删除作业流版本")
    @DeleteMapping("/delVersion")
    public Result delVersion(@ApiIgnore @LoginUser SysUser user,Long logId) {
        return processDefinitionService.delVersion(user,logId);
    }

    @ApiOperation(value = "删除作业流")
    @DeleteMapping("/delProcess")
    public Result delProcess(@ApiIgnore @LoginUser SysUser user,Long processId) {
        return processDefinitionService.delProcess(user,processId);
    }

    @ApiOperation(value = "复制作业流")
    @DeleteMapping("/copyProcess")
    public Result copyProcess(@ApiIgnore @LoginUser SysUser user,Long processId) throws SnowFlakeUtils.SnowFlakeException {
        return processDefinitionService.copyProcess(user,processId);
    }

    @ApiOperation(value = "作业流上下线")
    @GetMapping("/release")
    public Result release(@ApiIgnore @LoginUser SysUser user,Long processId) {
        return processDefinitionService.release(user,processId);
    }


    @ApiOperation(value = "树形图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "PROCESS_DEFINITION_CODE", required = true, dataType = "Long", example = "100"),
            @ApiImplicitParam(name = "limit", value = "LIMIT", required = true, dataType = "Int", example = "100")
    })
    @GetMapping(value = "/{code}/view-tree")
    public TreeViewEntity viewTree(@PathVariable("code") long code, @RequestParam("limit") Integer limit) {
        TreeViewEntity result = processDefinitionService.viewTree(code, limit);
        return result;
    }

}
